package com.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.project.entity.UnitInfo;
import com.project.service.UnitInfoManage;
import com.project.service.impl.UnitInfoManageService;
/**
 * 单位信息服务Web接口
 * 作者：甘雨
 **/
public class UnitManageAction {
	private UnitInfo unitInfo;
	private Integer unitId;
	private String start;
	private String limit;
	private String	inqueryType; //类型参数
	private String	inqueryText;//查询值参数
	


	private String jsonString;// 使用json传递数据
	private UnitInfoManageService unitInfoManageService;

	void clearValue() throws Throwable{
		limit = null;
		start = null;
		inqueryType = null;
		inqueryText = null;
		finalize();
	}
	/**
	 * 根据查询条件获得所有单位信息，包括分页
	 * update by sean
	 * @return
	 * @throws Throwable 
	 */
	public String getAll() throws Throwable{
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("limit", limit);
		if( inqueryType != null && !inqueryType.equals("") 
				&& inqueryText != null && !inqueryText.equals("")){
			map.put(inqueryType, inqueryText);
		}
		List<UnitInfo> unitList = this.unitInfoManageService.getAllUnit(map);
		JSONArray jsonObject = JSONArray.fromObject(unitList);
		this.setJsonString("{success:true,totalCount : " + this.unitInfoManageService.countUnit(map)
				+ ", list:" + jsonObject.toString() + "}");
		clearValue();
		return "success";
	}
	
	
	public String getById(){
		UnitInfo unitInfo = this.unitInfoManageService.getById(unitId);
		JSONArray jsonObject = JSONArray.fromObject(unitInfo);
		this.setJsonString("{success:true,totalCount : " + 1
				+ ", list:" + jsonObject.toString() + "}");
		this.unitId = null;
		return "success";
	}
	
	public String insertUnit(){
		this.unitInfoManageService.InsertUnitInfo(unitInfo);
		JSONArray jsonObject = JSONArray.fromObject(unitInfo);
		this.setJsonString("{success:true,totalCount : " + 1
				+ ", list:" + jsonObject.toString() + "}");
		this.unitInfo = null;
		return "success";
	}
	
	public String updateUnit(){
		if(unitInfo.getUnitid()!=null){
			this.unitInfoManageService.updateUnitInfo(unitInfo);
			JSONArray jsonObject = JSONArray.fromObject(unitInfo);
			this.setJsonString("{success:true,totalCount : " + 1
					+ ", list:" + jsonObject.toString() + "}");
		}else{
			System.out.println("单位信息更新不成功！");
			this.setJsonString("{success:false" +  "}");
		}
		this.unitInfo = null;
		return "success";
	}
	
	
	public String conditionSelect() throws Throwable{
		if(this.unitInfo ==null){
			System.out.println("单位信息参数传递错误");
			return "error";
			}
		else{
			List<UnitInfo> unitList = this.unitInfoManageService.conditionSelect(unitInfo, Integer.valueOf(start), Integer.valueOf(limit));
			JSONArray jsonObject = JSONArray.fromObject(unitList);
			this.setJsonString("{success:true,totalCount : " + unitList.size()
					+ ", list:" + jsonObject.toString() + "}");
			clearValue();
			return "success";
		}
	}
	
	//getter and setter~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	public UnitInfo getUnitInfo() {
		return unitInfo;
	}

	public void setUnitInfo(UnitInfo unitInfo) {
		this.unitInfo = unitInfo;
	}

	public Integer getUnitId() {
		return unitId;
	}

	public void setUnitId(Integer unitId) {
		this.unitId = unitId;
	}



	public String getStart() {
		return start;
	}


	public void setStart(String start) {
		this.start = start;
	}


	public String getLimit() {
		return limit;
	}


	public void setLimit(String limit) {
		this.limit = limit;
	}


	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}


	public UnitInfoManageService getUnitInfoManageService() {
		return unitInfoManageService;
	}


	public void setUnitInfoManageService(UnitInfoManageService unitInfoManageService) {
		this.unitInfoManageService = unitInfoManageService;
	}



	public String getInqueryType() {
		return inqueryType;
	}


	public void setInqueryType(String inqueryType) {
		this.inqueryType = inqueryType;
	}



	public String getInqueryText() {
		return inqueryText;
	}


	public void setInqueryText(String inqueryText) {
		this.inqueryText = inqueryText;
	}
	
}
